package cn.dnui_lxx432.dnuifood_16110100432.controller;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import cn.dnui_lxx432.dnuifood_16110100432.JavaBean.UserBean;
import cn.dnui_lxx432.dnuifood_16110100432.R;
import cn.dnui_lxx432.dnuifood_16110100432.listener.OnRetrofitListener;
import cn.dnui_lxx432.dnuifood_16110100432.model.RetrofitBaseModel;
import cn.dnui_lxx432.dnuifood_16110100432.model.UserModel;

public class LoginActivity extends BaseActivity
        implements OnRetrofitListener<UserBean>{
private EditText username;
private EditText userpass;
private Button btn_login;
private Button btn_reg;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    void initViews() {
        setLayout(R.layout.activity_login);
        username=(EditText)findViewById(R.id.username);
        userpass=(EditText)findViewById(R.id.userpass);
        btn_login=(Button)findViewById(R.id.button_login);
        btn_reg=(Button)findViewById(R.id.button_register);


    }

    @Override
    void initEvents() {

        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (username.getText().toString().trim().equals("")||userpass.getText().toString().trim().equals("")){
                    Toast.makeText(LoginActivity.this,"用户名和密码不能为空",Toast.LENGTH_SHORT).show();
                }
                else{
                    login();
                }

            }
        });
        btn_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setClass(LoginActivity.this,RegActivity.class);
                startActivityForResult(intent,1);
            }
        });

    }

    protected void onActivityResult(int requestCode,int resultCode,Intent data){
        super.onActivityResult(requestCode,resultCode,data);
        String et_user=data.getStringExtra("user");
        username.setText(et_user);
    }

    @Override
    void initData() {

    }
    void login(){
        UserModel userModel=new UserModel();
        userModel.login(username.getText().toString().trim(),userpass.getText().toString().trim(),this);
    }
    @Override
    public void onSuccess(UserBean userId,int flag) {
        if(userId.getUserid().equals("0")){
            Toast.makeText(LoginActivity.this,"用户名或密码错误",Toast.LENGTH_SHORT).show();
        }
        else{
            saveUser(userId.getUserid());
            Intent intent=new Intent(LoginActivity.this,MainActivity.class);
            startActivity(intent);
            Toast.makeText(LoginActivity.this,"登陆成功",Toast.LENGTH_SHORT).show();
        }


    }

    @Override
    public void onFailure(String msg) {
        Toast.makeText(LoginActivity.this,"网络错误",Toast.LENGTH_SHORT).show();


    }


    public void saveUser(String user_id) {
        SharedPreferences sharedPreferences = getSharedPreferences("lxx", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();//获取编译器
        editor.putString("username", username.getText().toString());
        editor.putString("user_id", user_id);
        editor.putString("userpass", userpass.getText().toString());
        editor.commit();
    }
}
